package com.glink.common.util;

import org.apache.commons.lang3.StringUtils;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

/**
 * @author wg
 */
public class EncoUtils {
    /**
     * salt值，和bootstrap-dev.yml中的保持一致
     */
    private static final String PWD_STR = "abc!@#123";

    public static void main(String[] args) {
//       System.out.println(decrypt("2KiqXld7IVm5gFI3G/9j4ayEaclItMRN3R1zgM8t/cEs+cQwd57ykfYp4NijvLmJDLJjLsZ13zYNBtf/vO1mRiuY87F0MUHtQ3tOz3bE3aM="));
        encryptor("192.168.1.31|root|4r5t6y$R%T^Y|13406|smart_construction_site|com.glink.common.contants");
    }

    public static String[] parseFromStr(String str) {
        return decrypt(str).split("[|]");
    }

    /**
     * 将加密串解析成明文
     * <p>
     * 算法：PBEWithMD5AndDES
     */
    private static String decrypt(String str) {
        System.out.println("获取到密文：  " + str);
        StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
        decryptor.setPassword(PWD_STR);
        String s = decryptor.decrypt(str);
        System.out.println("解析出明文：  " + StringUtils.substringBefore(s, "|") + "*****************");
        return s;
    }

    @SuppressWarnings("unused")
    private static String encryptorFromDbInfo(String... arr) {
        System.out.println("获取到数据库参数：  " + arr);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);

            if (i != (arr.length - 1)) {
                sb.append("|");
            }
        }

        return getEncryptString(sb.toString());
    }

    private static String getEncryptString(String s2) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(PWD_STR);
        String s = encryptor.encrypt(s2);
        System.out.println("加密的密文：  " + s);
        return s;
    }

    /**
     * 加密给定的字符串
     * <p>
     * 算法：PBEWithMD5AndDES
     */
    @SuppressWarnings("unused")
    private static String encryptor(String str) {
        System.out.println("获取到明文：  " + str);
        return getEncryptString(str);
    }
}